﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DTS
{
    class AlgorithmSMA : Algorithm
    {
        int Period;
        Double factor = 1.03;
        public AlgorithmSMA(int days)
        {
            Period = days;
            this.name = "SMA"+days.ToString();
        }
        public AlgorithmSMA()
        {
            Period = 100;
            this.name = "SMA100";
        }
        public void setFactor(Double val)
        {
            factor = val;
        }
        public override int getDecision(HistoriaSpolki hs, DateTime Today)
        {
            var hsPrep = hs.getPrevOfData(Today, Period);
            if (hsPrep.getDane().Count == 0) return 0;
            List<Double> wartosc = new List<double>();
            foreach (var tmp in hsPrep.getDane())
                wartosc.Add(tmp.GetClose());
            wartosc.Reverse();
            Double wynikSMA = Pomocnik.SMA(wartosc);
            if (wartosc[wartosc.Count - 1] > wynikSMA * factor && wartosc[wartosc.Count - 2] < wynikSMA * factor) 
                return 1;
            if (wartosc[wartosc.Count - 1] < wynikSMA * (1 / factor) && wartosc[wartosc.Count - 2] > wynikSMA * (1 / factor)) 
                return -1;
            return 0;
        }
        public override int getDecision(HistoriaSpolki hs, double cena, DateTime Today)
        {
            var hsPrep = hs.getPrevOfData(Today, Period);
            if (hsPrep.getDane().Count == 0) return 0;
            List<Double> wartosc = new List<double>();
            foreach (var tmp in hsPrep.getDane())
                wartosc.Add(tmp.GetClose());
            wartosc.Reverse();
            wartosc.Add(cena);
            Double wynikSMA = Pomocnik.SMA(wartosc);
            if (cena > wynikSMA * factor && wartosc[wartosc.Count - 1] < wynikSMA * factor)
                return 1;
            if (cena < wynikSMA * (1 / factor) && wartosc[wartosc.Count - 1] > wynikSMA * (1 / factor))
                return -1;
            return 0;
        }
    }
}
